package com.szrt.socket.ch03.udp.demo01;


import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

import org.junit.Test;

public class T4 {
	
	/**
	 * 发送端
	 * @throws Exception
	 */
	@Test
	public void send() throws Exception {
		
		DatagramSocket ds=new DatagramSocket();
		
		Scanner sc=new Scanner(System.in);
		System.out.println("输入你的消息：");
		String nt = sc.next();
		
	
		byte[] b=nt.getBytes();
		//创建一个数据报，注意每个数据报不能大于64k,
		DatagramPacket pack=new DatagramPacket(b,0,b.length,InetAddress.getByName("192.168.1.48"),8899);
		ds.send(pack);
		ds.close();
	
	}

	
	/**
	 * 接收端
	 * @throws Exception
	 */
	@Test
	public void reveive() throws Exception {
		
		
		DatagramSocket ds=new DatagramSocket(8899);
		//接收到的数据保存在数组b中
		byte[] b=new byte[1024];
		DatagramPacket pack=new DatagramPacket(b,0,b.length);
		ds.receive(pack);
		
		String jg=new String(pack.getData(),0,pack.getLength());
		
		System.out.println(jg);
		
	
	}
}
